<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="form.type === 1?'修改邀请人ID':'修改团队ID'" width="600px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
      <el-form-item :label="form.type === 1?'邀请人id':'团队id'" prop="customerId">
        <el-input v-model.number="form.customerId" style="width: 370px;"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="loading" type="primary" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateInviterId } from '@/api/mall/customer'
export default {
  components: { },
  data() {
    return {
      loading: false,
      dialog: false,
      form: {
        customerId: '',
        ids: [],
        type: '',
        customerIdList: []
      },
      rules: {
        customerId: [
          { required: true, message: '请输入ID', trigger: 'blur', type: 'number' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      // 表单验证
      this.$refs['form'].validate((valid) => {
        // 通过
        if (valid) {
          this.doAdd()
        }
      })
    },
    doAdd() {
      this.loading = true
      this.form.customerIdList = this.form.ids
      updateInviterId(this.form).then(res => {
        if (!res.result) {
          this.$message({
            message: res.content,
            type: 'warning'
          })
        } else {
          this.$notify({
            title: res.content,
            type: 'success',
            duration: 2500
          })
          // 执行成功
          this.resetForm()
          this.$parent.init()
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        customerId: ''
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }

</style>
